<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">类型</label>
        <el-select v-model="query.type" clearable filterable placeholder="类型" style="width: 185px;" class="filter-item">
          <el-option
            v-for="item in dict.BANNER_TYPE"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">是否有效</label>
        <el-select
          v-model="query.ifUse"
          clearable
          filterable
          placeholder="类型"
          style="width: 185px;"
          class="filter-item"
        >
          <el-option
            v-for="item in dict.status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">位置</label>
        <el-select
          v-model="query.positionType"
          clearable
          filterable
          placeholder="位置"
          style="width: 185px;"
          class="filter-item"
        >
          <el-option
            v-for="item in dict.BANNER_POSITION_TYPE"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-date-picker
          v-model="query.createTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="图片">
            <el-upload
              class="avatar-uploader"
              :action="qiNiuUploadApi"
              :headers="headers"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="位置" prop="positionType">
            <el-select v-model="form.positionType" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.BANNER_POSITION_TYPE"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.BANNER_TYPE"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="是否有效" prop="ifUse">
            <el-radio v-for="item in dict.status" :key="item.id" v-model="form.ifUse" :label="item.value">{{ item.label
            }}
            </el-radio>
          </el-form-item>
          <el-form-item label="请求参数">
            <el-input v-model="form.param" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" width="55" />
        <el-table-column prop="imageUrl" label="图片URL ">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.imageUrl"
              fit="contain"
            />
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型" width="120">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>
        <el-table-column prop="positionType" label="位置">
          <template slot-scope="scope">
            {{ dict.label.BANNER_POSITION_TYPE[scope.row.positionType] }}
          </template>
        </el-table-column>
        <el-table-column prop="ifUse" label="是否有效">
          <template slot-scope="scope">
            {{ dict.label.status[scope.row.ifUse] }}
          </template>
        </el-table-column>
        <el-table-column prop="param" label="请求参数" />
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-permission="['admin','ycBannerConf:edit','ycBannerConf:del']"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudYcBannerConf from '@/api/advertisement/ycBannerConf'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import crudQiNiu from '@/api/tools/qiniu'

const defaultForm = {
  id: null,
  type: null,
  ifUse: null,
  isDel: null,
  param: null,
  positionType: null,
  url: null,
  createTime: null
}
export default {
  name: 'YcBannerConf',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['BANNER_TYPE', 'status', 'BANNER_POSITION_TYPE'],
  cruds() {
    return CRUD({ title: '轮播图', url: 'api/ycBannerConf', sort: 'id,desc', crudMethod: { ...crudYcBannerConf }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'ycBannerConf:add'],
        edit: ['admin', 'ycBannerConf:edit'],
        del: ['admin', 'ycBannerConf:del']
      },
      rules: {
        type: [
          { required: true, message: '类型不能为空', trigger: 'blur' }
        ],
        ifUse: [
          { required: true, message: '是否有效不能为空', trigger: 'blur' }
        ],
        positionType: [
          { required: true, message: '位置不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'type', display_name: '类型' },
        { key: 'ifUse', display_name: '是否有效' },
        { key: 'positionType', display_name: '位置' }
      ],
      //
      headers: {
        'Authorization': getToken()
      },
      imageUrl: ''
    }
  },
  computed: {
    ...mapGetters([
      'qiNiuUploadApi'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeSubmit]() {
      console.log(this.form)
      return true
    },
    //
    handleAvatarSuccess(res, file) {
      console.log(res)
      console.log(file)
      this.imageUrl = res.data[0]
      // this.imageUrl = URL.createObjectURL(file.raw)
      console.log(this.imageUrl)
      this.form.imageUrl = this.imageUrl
    }
  }
}
</script>

<style scoped>
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 178px;
  }

  .avatar-uploader:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px!important;
    height: 178px!important;
    display: block;
  }
</style>
